package com.stan.script.handle.win;

import com.stan.script.handle.AbsStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author Junqiang Zhu
 * @date 2024-05-06 11:18
 */
@Service
public class WinScript extends AbsStrategy {

    public final Logger logger = LoggerFactory.getLogger(WinScript.class);


    @Override
    protected String scriptPath() {
        return "E:\\my\\project\\script\\push";
    }

    @Override
    protected String suffix() {
        return ".bat";
    }

    @Override
    public boolean handle() {
        try {
            String command = scriptPath() + suffix();
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                // 打印输出日志
                logger.info(line);
            }
            process.waitFor();
            return true;
        } catch (Exception e) {
            logger.error("win脚本执行错误", e);
            return false;
        }
    }

}
